<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition License
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magentocommerce.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Sales
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://www.magentocommerce.com/license/enterprise-edition
 */

/**
 * API2 class for orders (customer)
 *
 * @category   Mage
 * @package    Mage_Sales
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Sales_Model_Api2_Order_Rest_Customer_V1 extends Mage_Sales_Model_Api2_Order_Rest
{
    /**
     * Retrieve information about specified order item
     *
     * @throws Mage_Api2_Exception
     * @return array
     */
    protected function _retrieve()
    {
        $orderId = $this->getRequest()->getParam('id');
        $order = Mage::getModel('sales/order')->load($orderId);
        $orderData = array();
        $coreHelper = Mage::helper('core');
        $_imageHelper = Mage::helper('catalog/image');

        $orderData['increment_id'] = $order->getIncrementId();
        $orderData['status'] = $order->getStatus();
        $orderData['grand_total'] = $coreHelper->formatPrice($order->getGrandTotal(), false);
        $orderData['subtotal'] = $coreHelper->formatPrice($order->getSubtotal(), false);
        $orderData['shipping_amount'] = $coreHelper->formatPrice($order->getShippingAmount(), false);
        $orderData['created_at'] = $order->getCreatedAt();
        $orderData['shipping_method'] = $order->getShippingDescription();

        $shippingAddress = $order->getShippingAddress();
        $orderData['shipping_address'] = array(
            'name' => $shippingAddress->getFirstname() . ' ' . $shippingAddress->getLastname(),
            'address' => $shippingAddress->getCountry() . ' ' . $shippingAddress->getRegion() . ' ' . $shippingAddress->getCity() . ' ' . implode(' ', $shippingAddress->getStreet()),
            'telephone' => $shippingAddress->getTelephone()
        );

        foreach ($order->getAllItems() as $item) {
            $items = array();
            $items['item_id'] = $item->getItemId();
            $items['product_id'] = $item->getProductId();
            $items['name'] = $item->getName();
            $items['image'] = $_imageHelper->init($item->getProduct(), 'small_image')->resize(200) . "";
            $items['price'] = $coreHelper->formatPrice($item->getPrice(), false);
            $items['qty_ordered'] = $item->getQtyOrdered();
            $items['subtotal'] = $coreHelper->formatPrice($item->getRowTotalInclTax(), false);
            $orderData['items'][] = $items;
        }


        return $orderData;
    }

    /**
     * Get orders list
     *
     * @return array
     */
    protected function _retrieveCollection()
    {
        $collection = $this->_getCollectionForRetrieve();
        /*if ($this->_isPaymentMethodAllowed()) {
            $this->_addPaymentMethodInfo($collection);
        }
        if ($this->_isGiftMessageAllowed()) {
            $this->_addGiftMessageInfo($collection);
        }
        $this->_addTaxInfo($collection);*/

        $ordersData = array();
        $coreHelper = Mage::helper('core');

        foreach ($collection->getItems() as $order) {
            //$ordersData[$order->getId()] = $order->toArray();
            //print_r($order->getData());
            $data = array();
            $data['id'] = $order->getId();
            $data['increment_id'] = $order->getIncrementId();
            $data['status'] = $order->getStatus();
            $data['grand_total'] = $coreHelper->formatPrice($order->getGrandTotal(), false);
            $data['subtotal'] = $coreHelper->formatPrice($order->getSubtotal(), false);
            $data['shipping_amount'] = $coreHelper->formatPrice($order->getShippingAmount(), false);
            $data['created_at'] = $order->getCreatedAt();
            $ordersData[] = $data;
        }

        /*if ($ordersData) {
            foreach ($this->_getAddresses(array_keys($ordersData)) as $orderId => $addresses) {
                $ordersData[$orderId]['addresses'] = $addresses;
            }
            foreach ($this->_getItems(array_keys($ordersData)) as $orderId => $items) {
                $ordersData[$orderId]['order_items'] = $items;
            }
            foreach ($this->_getComments(array_keys($ordersData)) as $orderId => $comments) {
                $ordersData[$orderId]['order_comments'] = $comments;
            }
        }*/
        return $ordersData;
    }

    /**
     * Retrieve collection instance for orders
     *
     * @return Mage_Sales_Model_Resource_Order_Collection
     */
    protected function _getCollectionForRetrieve()
    {
        $collection = Mage::getResourceModel('sales/order_collection')->addAttributeToFilter(
            'customer_id', array('eq' => $this->getApiUser()->getUserId())
        );

        return $collection;
    }

    /**
     * Prepare and return order comments collection
     *
     * @param array $orderIds Orders' identifiers
     * @return Mage_Sales_Model_Resource_Order_Status_History_Collection|Object
     */
    protected function _getCommentsCollection(array $orderIds)
    {
        return parent::_getCommentsCollection($orderIds)->addFieldToFilter('is_visible_on_front', 1);
    }
}
